package com.jingtai.platform.service.supply.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.core.entity.supply.SupplyGoodsDetail;
import com.jingtai.core.mapper.supply.SupplyGoodsDetailMapper;
import com.jingtai.platform.service.supply.SupplyGoodsDetailService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * 供应商商品详情表 impl
 *
 * @author sunlaiqian
 * @date 2020-12-02 18:31:33
 */
@Service
public class SupplyGoodsDetailServiceImpl extends ServiceImpl<SupplyGoodsDetailMapper, SupplyGoodsDetail> implements SupplyGoodsDetailService {

    @Autowired
    private SupplyGoodsDetailService supplyGoodsDetailService;

    @Override
    public ResultUtil SupplyGoodsDetaillist(Integer page, Integer limit, String conditions){
        QueryWrapper<SupplyGoodsDetail> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,supplyGoodsDetailService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addSupplyGoodsDetail(SupplyGoodsDetail supplyGoodsDetail) {
        boolean save = supplyGoodsDetailService.save(supplyGoodsDetail);
        return new ResultUtil(save ? ResultMapInfo.ADDSUCCESS:ResultMapInfo.ADDFAIL);
    }

    @Override
    public ResultUtil getSupplyGoodsDetailById(Long id) {
        SupplyGoodsDetail supplyGoodsDetail = supplyGoodsDetailService.getById(id);
        return supplyGoodsDetail == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,supplyGoodsDetail);
    }

    @Override
    public ResultUtil delSupplyGoodsDetailById(Long id) {
        SupplyGoodsDetail supplyGoodsDetail = new SupplyGoodsDetail();
        supplyGoodsDetail.setId(id);
        supplyGoodsDetail.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = supplyGoodsDetailService.updateById(supplyGoodsDetail);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil getSupplyGoodsDetailByGoodsCode(String goodsCode) {
        SupplyGoodsDetail supplyGoodsDetail = supplyGoodsDetailService.getOne(Wrappers.lambdaQuery(SupplyGoodsDetail.class).eq(SupplyGoodsDetail::getGoodsCode,goodsCode));
        return supplyGoodsDetail == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,supplyGoodsDetail);
    }

    @Override
    public ResultUtil editSupplyGoodsDetail(SupplyGoodsDetail supplyGoodsDetail) {
        boolean editState = supplyGoodsDetailService.updateById(supplyGoodsDetail);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }
}
